<?php

declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://hyperf.wiki
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 */

namespace App\Common;

use App\Constants\Constants;
use App\Exception\ResponseException;
use Hyperf\AsyncQueue\Driver\DriverFactory;
use Hyperf\AsyncQueue\Driver\DriverInterface;
use Hyperf\Cache\Listener\DeleteListenerEvent;
use Hyperf\Di\Annotation\Inject;
use Hyperf\Filesystem\FilesystemFactory;
use Hyperf\Logger\LoggerFactory;
use Hyperf\Redis\Redis;
use Hyperf\Snowflake\IdGeneratorInterface;
use Hyperf\Utils\Context;
use League\Flysystem\Filesystem;
use Psr\Container\ContainerInterface;
use Psr\EventDispatcher\EventDispatcherInterface;
use Psr\Log\LoggerInterface;
use Psr\SimpleCache\CacheInterface;

/**
 * 基类.
 */
abstract class Base
{
    /**
     * @Inject
     */
    protected ContainerInterface $container;

    /**
     * @Inject
     */
    protected EventDispatcherInterface $eventDispatcher;

    /**
     * @Inject
     */
    protected CacheInterface $cache;

    /**
     * @Inject
     */
    protected Redis $redis;

    /**
     * @Inject
     */
    protected IdGeneratorInterface $snowflake;

    /**
     * 清除app账户缓存.
     */
    public function flushRoleCache(int $userId)
    {
        $this->redis->del('c:user:' . $userId);
        $this->redis->del('c:user:simple:' . $userId);
    }

    /**
     * 清除房间账户缓存.
     */
    public function flushRoomCache(int $userId, int $roomId)
    {
        $this->redis->del('c:chat_room:' . $userId);
        $this->redis->del('c:chat_room_rid:' . $roomId);
    }

    public function getUserId(): int
    {
        return (int) Context::get(Constants::CONTEXT_USER_ID, 0);
    }

    /**
     * 错误响应.
     */
    protected function error(int $code = 400, string $msg = ''): void
    {
        if ($code == 0) {
            $code = 400;
        }
        throw new ResponseException($msg, $code);
    }

    /**
     * 成功响应.
     *
     * @throws ResponseException
     */
    protected function success(array $data = []): void
    {
        Context::set('successful_data', $data);

        throw new ResponseException('success', 200);
    }

    /**
     * 消息队列投递.
     */
    protected function asyncQueue(string $channel = 'default'): DriverInterface
    {
        return $this->container->get(DriverFactory::class)->get($channel);
    }

    /**
     * 文件上传.
     */
    protected function upload(string $driver = 'oss'): Filesystem
    {
        return $this->container->get(FilesystemFactory::class)->get($driver);
    }

    /**
     * 日志管理.
     */
    protected function logger(string $channel, string $name = 'log'): LoggerInterface
    {
        return $this->container->get(LoggerFactory::class)->get($name, $channel);
    }

    /**
     * 清理缓存.
     */
    protected function flushCache(string $listener, array $args): void
    {
        $this->eventDispatcher->dispatch(new DeleteListenerEvent($listener, $args));
    }
}
